﻿
package com.ostrich.project.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.page.Pager;
import com.base.util.DateUtil;
import com.base.util.HtmlUtil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.ostrich.project.bean.TostPjProjcttab;
import com.ostrich.project.service.impl.TostPjProjcttabService;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostPjProjcttab")
public class TostPjProjcttabController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostPjProjcttabController.class);

	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjProjcttabService<TostPjProjcttab> tostPjProjcttabService; 
	
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView  list(TostPjProjcttab tostPjProjcttab,HttpServletRequest request) throws Exception{
		Map<String,Object>  context = getRootMap();
		List<TostPjProjcttab> dataList = tostPjProjcttabService.queryByList(tostPjProjcttab);
		//设置页面数据
		context.put("dataList", dataList);
		return forword("project/tabTostPjProjcttab",context); 
	}
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList")
	public void  dataList(TostPjProjcttab tostPjProjcttab,HttpServletResponse response) throws Exception{
		List<TostPjProjcttab> dataList = tostPjProjcttabService.queryByList(tostPjProjcttab);
				//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",tostPjProjcttab.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}

	@RequestMapping("/getProject")
	public void  getProject(TostPjProjcttab tostPjProjcttab,HttpServletResponse response) throws Exception{
		List<TostPjProjcttab> dataList = tostPjProjcttabService.queryAll(tostPjProjcttab);
				//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",dataList!=null?dataList.size():0);
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}

	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	
	@RequestMapping("/save")
	public ModelAndView save(TostPjProjcttab tostPjProjcttab,HttpServletRequest request,HttpServletResponse response) throws Exception{
		if(tostPjProjcttab.getId() == null){
			// 获取从页面传来的session(用户名)
			tostPjProjcttab.setCreatetime(DateUtil.getNowPlusTime());
			tostPjProjcttab.setPerson(SessionUtils.getUser(request).getEmail());
			tostPjProjcttabService.add(tostPjProjcttab);
			//sendSuccessMessage(response, "添加成功~");
		}else{
			tostPjProjcttabService.update(tostPjProjcttab);
		}
		//sendSuccessMessage(response, "保存成功~");
		return new ModelAndView("redirect:list.do");
		
	}
	
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostPjProjcttab bean  = tostPjProjcttabService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(Integer[] id,HttpServletResponse response) throws Exception{
		tostPjProjcttabService.delete(id);
		//sendSuccessMessage(response, "删除成功");
		return new ModelAndView("redirect:list.do");
	}
	@RequestMapping("checkPjcode")  
	public void chenckPjcode(TostPjProjcttab projct,HttpServletResponse response) throws Exception{
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		List<TostPjProjcttab> list = tostPjProjcttabService.queryByList(projct);
		if(list.size() > 0){
			out.print(-1);
		}
		else out.print(1);
			out.flush();
			out.close();
	}
	
}
